package top.timestreet.xmsk.qu.processor;

import java.util.ArrayList;
import java.util.List;

import org.springframework.stereotype.Service;

import top.timestreet.xmsk.base.pojo.SearchPage;
import top.timestreet.xmsk.base.processor.BaseSite;
import top.timestreet.xmsk.util.ChapterMeta;
import top.timestreet.xmsk.util.DESUtil;
import top.timestreet.xmsk.util.URLConvertUtil;
import us.codecraft.webmagic.Page;
import us.codecraft.webmagic.Site;
import us.codecraft.webmagic.processor.PageProcessor;
import us.codecraft.webmagic.selector.Selectable;

/**
* @author TateLiao
* @data 2019年6月2日
* 笔趣阁(https://www.qu.la/)搜索页面抓取规则
* egPage: https://sou.xanbhx.com/search?siteid=qula&q=剑徒之路
*/
@Service
public class QuSearchPageProcessor extends BaseSite implements PageProcessor {
	
	@Override
	public void process(Page page) {
		List<SearchPage> searchPages = new ArrayList<>();
		List<Selectable> nodes = page.getHtml().xpath("//*[@id=\"search-main\"]/div/ul/li").nodes();
		// 从1开始解析内容，因为第0行为类型标题
		for (int i = 1; i < nodes.size(); i++) {
			Selectable selectable = nodes.get(i);
			SearchPage searchPage = new SearchPage();
			searchPage.setName(selectable.xpath("//*/span[2]/a/text()").toString());
			searchPage.setNameURL(URLConvertUtil.uRLConvert(DESUtil.encrypt(selectable.xpath("//*/span[2]/a/@href").toString())));
			searchPage.setLastChapter(selectable.xpath("//*/span[3]/a/text()").toString());
			searchPage.setLastChapterUrl(URLConvertUtil.uRLConvert(DESUtil.encrypt(selectable.xpath("//*/span[3]/a/@href").toString())));
			searchPage.setAuthor(selectable.xpath("//*/span[4]/text()").toString());
			searchPage.setLastUpdateTime(
					ChapterMeta.formatSearchPageLastUpdateTime(
							selectable.xpath("//*/span[6]/text()").toString()));
			searchPage.setStatus(selectable.xpath("//*/span[7]/text()").toString());
			searchPages.add(searchPage);
			page.putField("list", searchPages);
		}
	}

	@Override
	public Site getSite() {
		return site;
	}

}
